home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter4 / propshet.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  6KB  |  241 lines

  1.  
  2. #include <windows.h>  
  3. #include "propshet.h"  
  4. #include "commctrl.h"
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "PropertySheet()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107. LRESULT CALLBACK Page1Proc( HWND hDlg, UINT message,        
  108.                             WPARAM wParam, LPARAM lParam )
  109. {
  110.    switch (message) 
  111.    {
  112.        case WM_INITDIALOG: 
  113.                return (TRUE);
  114.    }
  115.  
  116.    return( FALSE );
  117. }
  118.  
  119.  
  120. LRESULT CALLBACK Page2Proc( HWND hDlg, UINT message,        
  121.                             WPARAM wParam, LPARAM lParam )
  122. {
  123.    switch (message) 
  124.    {
  125.        case WM_INITDIALOG: 
  126.                return (TRUE);
  127.    }
  128.  
  129.    return( FALSE );
  130. }
  131.  
  132.  
  133. LRESULT CALLBACK Page3Proc( HWND hDlg, UINT message,        
  134.                             WPARAM wParam, LPARAM lParam )
  135. {
  136.    switch (message) 
  137.    {
  138.        case WM_INITDIALOG: 
  139.                return (TRUE);
  140.    }
  141.  
  142.    return( FALSE );
  143. }
  144.  
  145.  
  146. VOID DisplayProperties( HWND hWnd )
  147. {
  148.    PROPSHEETPAGE   psp;
  149.    PROPSHEETHEADER psh;
  150.    HPROPSHEETPAGE  hpsp[3];
  151.  
  152.    psp.dwSize    = sizeof( PROPSHEETPAGE );
  153.    psp.dwFlags   = PSP_DEFAULT;
  154.    psp.hInstance = hInst;
  155.  
  156.    psp.pszTemplate = "PAGE1";
  157.    psp.pfnDlgProc  = (DLGPROC)Page1Proc;
  158.    hpsp[0] = CreatePropertySheetPage( &psp );
  159.  
  160.    psp.pszTemplate = "PAGE2";
  161.    psp.pfnDlgProc  = (DLGPROC)Page2Proc;
  162.    hpsp[1] = CreatePropertySheetPage( &psp );
  163.  
  164.    psp.pszTemplate = "PAGE3";
  165.    psp.pfnDlgProc  = (DLGPROC)Page3Proc;
  166.    hpsp[2] = CreatePropertySheetPage( &psp );
  167.  
  168.    memset( &psh, 0, sizeof( PROPSHEETHEADER ) );
  169.    psh.dwSize     = sizeof( PROPSHEETHEADER );
  170.    psh.dwFlags    = PSH_USEICONID;
  171.    psh.hInstance  = hInst;
  172.    psh.hwndParent = hWnd;
  173.    psh.pszIcon    = "SMALL";
  174.    psh.nPages     = 3;
  175.    psh.phpage     = hpsp;
  176.    psh.pszCaption = "Properties";
  177.  
  178.    PropertySheet( &psh );
  179. }
  180.  
  181.  
  182. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  183. {
  184.    switch( uMsg )
  185.    {
  186.       case WM_CREATE :
  187.               InitCommonControls();
  188.               break;
  189.  
  190.       case WM_COMMAND :
  191.               switch( LOWORD( wParam ) )
  192.               {
  193.                  case IDM_TEST :
  194.                         DisplayProperties( hWnd );
  195.                         break;
  196.  
  197.                  case IDM_ABOUT :
  198.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  199.                         break;
  200.  
  201.                  case IDM_EXIT :
  202.                         DestroyWindow( hWnd );
  203.                         break;
  204.               }
  205.               break;
  206.       
  207.       case WM_DESTROY :
  208.               PostQuitMessage(0);
  209.               break;
  210.  
  211.       default :
  212.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  213.    }
  214.  
  215.    return( 0L );
  216. }
  217.  
  218.  
  219. LRESULT CALLBACK About( HWND hDlg,           
  220.                         UINT message,        
  221.                         WPARAM wParam,       
  222.                         LPARAM lParam)
  223. {
  224.    switch (message) 
  225.    {
  226.        case WM_INITDIALOG: 
  227.                return (TRUE);
  228.  
  229.        case WM_COMMAND:                              
  230.                if (   LOWORD(wParam) == IDOK         
  231.                    || LOWORD(wParam) == IDCANCEL)    
  232.                {
  233.                        EndDialog(hDlg, TRUE);        
  234.                        return (TRUE);
  235.                }
  236.                break;
  237.    }
  238.  
  239.    return (FALSE); 
  240. }
  241.